package com.ug.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ug.common.cache.redis.RedisCacheService;
import com.ug.common.constant.CommonConstants;
import com.ug.system.entity.vo.TableInfoVO;
import com.ug.system.mapper.ShowTablesMapper;
import com.ug.system.service.ShowTablesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: LY
 * @date: 2024/12/30 11:09
 * @description: 获取表信息接口实现
 */
@Service
@RequiredArgsConstructor
public class ShowTablesServiceImpl extends ServiceImpl<ShowTablesMapper, Object>implements ShowTablesService {

    private final RedisCacheService redisCacheService;

    @Override
    public List<TableInfoVO> getAllTableInfoList() {
        List<TableInfoVO> tableInfoList = (List<TableInfoVO>) redisCacheService.getObject(CommonConstants.TABLES_CACHE);
        if (tableInfoList == null){
            tableInfoList = new ArrayList<>();
            List<String> tables = baseMapper.getTables();
            for (String table : tables) {
                tableInfoList.add(new TableInfoVO(table, baseMapper.getTableInfo(table)));
            }
            redisCacheService.put(CommonConstants.TABLES_CACHE, tableInfoList, 12, TimeUnit.HOURS);
        }
        return tableInfoList;
    }
}
